﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace NMF.Languages.Reflection
{
    public class ReflectiveProperty : IProperty
    {
        internal ReflectiveProperty(PropertyInfo property, IType type, ReflectiveClass parent)
        {
            MappedProperty = property;
            this.type = type;
            this.parent = parent;
        }

        public PropertyInfo MappedProperty { get; private set; }
        private IType type;
        private ReflectiveClass parent;

        public IType Type
        {
            get { return type; }
        }

        public void SetAPrioriType(IType type)
        {
        }

        public string Name
        {
            get { return MappedProperty.Name; }
        }

        public object GetValue(object source)
        {
            return MappedProperty.GetValue(source, null);
        }

        public void SetValue(object source, object value)
        {
            MappedProperty.SetValue(source, value, null);
        }

        public IClass DeclaringClass
        {
            get { return parent; }
        }

        public bool IsCollectionProperty
        {
            //TODO: besseres Argument finden!
            get { return typeof(IList).IsAssignableFrom(MappedProperty.PropertyType); }
        }

        public void AssertProperty(IType propertyType)
        {
        }

        public void AssertIsValidFor(IClass declaringClass)
        {
        }


        public bool IsReadOnly
        {
            get { return !MappedProperty.CanWrite; }
        }
    }
}
